<?php

// Namespace

namespace Loop;


// Dependencies

require_once(PATH_LOOP . 'Class/CDocumentation.php');
require_once(PATH_LOOP . 'Class/CTarDriver.php');


/**
 * @package		Loop
 * @category	Class
 */

class CIODocumentationGenerator
{
	/** 
	 * Generate the I/O documentation
	 *
	 */

	public function api_generate()
	{
		// Call api

		$result = $this->generate();
	}



	/** 
	 * Generate the I/O documentation
	 *
	 */

	public function generate()
	{
		// Get the extension ids

		$extensionsIds = app()->getExtensionManager()->getExtensionsIds();

		// Get extensions that are shared

		$allExtensionsIds = app()->getExtensionManager()->getExtensionsIds();
		$nbExtensions = count($allExtensionsIds);

		for ($i = 0; $i < $nbExtensions; $i++)
		{
			$extensionId = $allExtensionsIds[$i];

			if (pref('[' . $extensionId . ']/database/isShared') == true)
			{
				if (isset($extensionsIds[$i]) === true)
				{
					unset($extensionsIds[$i]);
				}
			}
		}
		

		// Get the result

		$resultDocumentation = $this->getIOByExtension($extensionsIds);

		
		foreach ($resultDocumentation as $extensionId => $models)
		{
			foreach ($models as $model => $details)
			{
				$content = app()->getDocumentationHelper()->generateHtmlIO($details);

				// Get path to tthe file

				$pathTofile = path
				(
					array
					(
						PATH_TEMPORARY,
						'IO',
						$extensionId
					),
					true
				);

				$pathTofile = $pathTofile . $model . '.html';	


				// Generate

				$this->generateDocumentation($model, $content, $pathTofile)	;
			}
		}

		// Create new object

		$tarDriver = new \Loop\CTarDriver();


		// Create archive

		$tarDriver->save
		(
			path
			(
				array
				(
					PATH_TEMPORARY
				)
			) ,
			'IO',
			'IO.tar'
		);
	}


	/** Generate the html documentation
	 *
	 * @param string $documentationAPI 		The html documentation
	 * @param string $pathToFile 			The path to the html file
	 */

	private function generateDocumentation($className, $content, $pathTofile)
	{
		// Get the style sheets

		$urisOfStylesheets = pref('[Loop]/views/urisOfStylesheets');
		$sheet = '';

		if (is_array($urisOfStylesheets) === true)
		{
			foreach ($urisOfStylesheets as $uriOfStylesheet)
			{
				$sheet .= '<link rel="stylesheet" type="text/css" href="' . url($uriOfStylesheet) . '" />';
			}
		}


		// Construct html

		$documentation = 
'
<html>
	<head>
	' . $sheet  .'
	</head>
	<body>
		<div class="container" style="font-size: 13px; line-height: 19px;">
			<h4 style="text-align: center;">
				' . $className . ' 
			</h4>
			<div class="tabbable">
				<ul class="nav nav-tabs" id="myTab">	
					<li class="active">
						<a href="#io" data-toggle="tab">I/O</a>
					</li>			
				</ul>
			</div>
			<div class="tab-content">				
				<div class="tab-pane active" id="io">
					' . $content. '
				</div>
			</div>
		</div>
	</body>
</html>';


		// Generate the file

		file_put_contents($pathTofile, $documentation);
	}


	/**
	 * Get the all the i/o description
	 *
	 * @param array $extensionIds 	The extension ids
	 * 
	 * @result array 	The result by extension and by model
	 */

	private function getIOByExtension($extensionsIds)
	{
		// Initialize variable

		$documentation = new CDocumentation();
		$resultDocumentation = array();


		foreach ($extensionsIds as $extensionId)
		{
			//

			$models = array();
			$namespaces = array();

			$namespace = str_replace('-', '\\', $extensionId);

			$pathToExtension = app()->getPathHelper()->convertExtensionIdToPath($extensionId) . 'Model/';
			$models[$pathToExtension] = app()->getModelManager()->getModels($namespace, $pathToExtension);
			$namespaces[$pathToExtension] = $namespace;

			
			// Instanciate each classes 

			foreach ($models as $path => $classNames) 
			{
				foreach ($classNames as $className)
				{

					// Create the object as long as it's not abstract
					
					require_once($path . $className . '.php');

					

					// Does the class exist?

					if (class_exists($className) == false)
					{
						
						$objectName = $namespaces[$path]  . '\\' . $className;
					}
					else
					{
						$objectName = $className;
					}


					// Is it abstract?
						
					$objectReflection = new \ReflectionClass($objectName);
						
					
					if ($objectReflection->isAbstract() == true)
					{
						continue;
					}

					$object = new $objectName();
					

					$result = array 
					(
						'directory' => 'Model',
						'className' => $objectName
					);

					$documentation->getIO($objectReflection, $result);

					$resultDocumentation[$extensionId][$className] = $result;
				}
			}
		}	


		return $resultDocumentation;
	}
}
?>